package ch.wanandroid.lib.net;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static ch.wanandroid.lib.net.Constants.BASE_URL;

public class RetrofitClient {

    private volatile static Retrofit mRetrofit;

    public static Retrofit getInstance() {
        if (mRetrofit == null){
            synchronized (RetrofitClient.class){
                if (mRetrofit == null){
                    mRetrofit = create();
                }
            }
        }
        return mRetrofit;
    }

    private static Retrofit create() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.readTimeout(10, TimeUnit.SECONDS);
        builder.connectTimeout(9, TimeUnit.SECONDS);
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(builder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }
}
